'use strict'

/*在拦截器中初始化数据，会多次同步初始化数据 导致bug，；暂时未找到初始化数据合适位置
 * ipCookie官网 https://github.com/ivpusic/angular-cookie
 */
yjx.factory('PageInterceptor', ["$q", "$rootScope", "ipCookie", '$timeout', "$location",
    function ($q, $rootScope, ipCookie, $timeout, $location) {
        return {
            //配置config数据
            request: function (config) {
                //config.headers["TOKEN"] = $rootScope.user.token;
                //设置全局变量 包括cookie中的值
                //导航栏
                if (!$rootScope.viewPath) {
                    $rootScope.viewPath = "index";
                }

                var path = $location.path();
                //更新url路径
                if ($rootScope.path != $location.path()) {
                    $timeout(function () {
                        //console.info(path);
                        $rootScope.path = path
                        $rootScope.path1 = path.split('/')[1];
                        $rootScope.path2 = path.split('/')[2];
                        $rootScope.path3 = path.split('/')[3];
                        $rootScope.path4 = path.split('/')[4];
                    });
                }

                //获取当前url路径。（不能获取到访问页面路径！）
                //初始值判断
                var noUser = typeof(ipCookie('user')) == 'undefined';
                var noHeadPic = typeof ($rootScope.avatar) == 'undefined'

                if (noHeadPic) {
                    $rootScope.avatar = ipCookie('avatar');
                }

                //alert(noNs+'$'+noSs+'||'+ipCookie('NavStatus')+'$'+ipCookie('SideStatus'));
                //此部分需放入登录拦截器 假数据初始化
                if (!noUser) {
                    $timeout(function () {
                        $rootScope.username = ipCookie('username');
                        $rootScope.user = ipCookie('user');
                    })
                }
                ;

                //console.log($rootScope);
                return config;
            },
            //服务器返回数据检验
            responseError: function (response) {
                return $q.reject(response);
            }

        };
    }]);